angular.module('pay.controller', ['pay.service'])
    .controller("PayCtrl", function ($scope, $ionicModal, $rootScope,  $http) {

        ///提交-----------未完成(密码框未实现)


        var CCSessionId =localStorage.getItem("CCSessionId");
        var port = '/account/account/1.json'//接口
        var port2 = '/account/account/2.json'//接口
        var port3 = '/jsye/jsye/0/deleteConfirm.json'//接口
        var para = '';//参数
        var url =$rootScope.myglobal.serverurl+port+'?CCSessionId='+CCSessionId
        var url2 =$rootScope.myglobal.serverurl+port2+'?CCSessionId='+CCSessionId



        $scope.modal1 = function (payor) {
            console.log(payor, typeof payor);
            //判断是收款人还是付款人
            if (payor === 1) {
                $http.get(url)
                    .success(function (data1) {
                        console.log(data1);

                        $scope.clientSideList = data1;

                    });
                $scope.createContact = function (a) {
                    $scope.modal.hide();
                    //选择目标时将信息反馈的input栏中
                    $scope.data0 = a;
                    console.log($scope.data0);
                };
            } else if (payor === 2) {
                $http.get(url2)
                    .success(function (data1) {
                        console.log(data1);
                        $scope.clientSideList = data1;

                    });
                $scope.createContact = function (a) {
                    $scope.modal.hide();
                    //选择目标时将信息反馈的input栏中
                    $scope.paydata = a;
                    console.log($scope.paydata);

                };
            }

        };

//重置
        $scope.resize = function () {
            $scope.data0 = {};
            $scope.paydata = {};
            $scope.payermoney = ''
        };

////点击查询将结果反馈到查询栏中
        $scope.checkbalance = function (id0) {

            if (id0 === undefined) {
                alert('请选择付款账号')
            }
            var url3 =$rootScope.myglobal.serverurl+port3+'?CCSessionId='+CCSessionId+'&acctIds=' + id0 +
                '&payStrategy=0&amountGroupId=0'
            $http.get(url3 )
                .success(function (data2) {
                    if (data2.accountamount == '' || data2.accountamount == null) {
                        alert('无账户余额')
                    }
                    console.log(data2);

                })
        };

//------------------
        $ionicModal.fromTemplateUrl('templates/payaccount.html', {
            scope: $scope
        }).then(function (modal) {
            $scope.modal = modal;

        });
///提交-----------未完成(密码框未实现)
        $scope.submit = function () {
            console.log(!$scope.data0);
            /*虽然在$scope.pwchange  对付款功能做了限制
             但当刷新时  付款功能即便时

             */
            if (!$scope.data0 || !$scope.paydata || !$scope.payermoney) {
                alert('付款账号 收款账号 付款金额 不能为空！');
                return
            }


            //点击提交出现支付密码框
            $scope.bgpw = {
                display: "block"
            };
            //点击取消和其他地方输入框消失
            $scope.cancel = function () {
                $scope.bgpw = {
                    display: "none"
                }
            };
            //
            var passw = null;
            $scope.pwchange = function () {
                console.log(1, $scope.pwvalue);

                passw = $scope.pwvalue;
                //  当密码框有输入值时   取消disabled属性  没有时添加
                if (passw != '') {
                    $('#adddisabled').removeAttr('disabled');
                } else {
                    $('#adddisabled').attr('disabled', 'disabled')
                }


            };
            // 实现付款转帐的表单提交

            //付款人id
            var payer = $scope.data0.id;
            //收款人id
            var payee = $scope.paydata.id;
            //付款金额
            var money = $scope.payermoney;


            //点击付款
            $scope.payment = function () {
                //密码清空
                $scope.pwvalue = '';


                console.log(passw);
                //当付款后无论是否密码正确密码框应消失
                $scope.bgpw = {
                    display: "none"
                };
                //输入密码
                var port4="/fk/fk.json"
                var url4 =$rootScope.myglobal.serverurl+port4+'?CCSessionId='+CCSessionId
                //'http://192.168.50.9:8080/JMCC/fk/fk.json?' +
                //'CCSessionId=' + sesionid +
                $http.post( url4+'&payPassword=' + passw + '&systemFlag=2')
                    .success(function (data2) {
                        console.log(data2);
                        if (data2.error) {
                            alert(data2.error);
                            return
                        }
                        alert(data2.msg);
                        console.log(payer, payee, money);

                        //当密码正确时才执行提交

                        var url5 =$rootScope.myglobal.serverurl+port4+'?CCSessionId='+CCSessionId
                        $http.post(url5+ '&payerAcctId=' + payer +
                                '&amount=' + money + '&payeeAcctId=' + payee)
                            .success(function (data2) {
                                console.log(data2);
                                if (data2.error) {
                                    alert(data2.error);
                                }

                            })

                    })

            }


        }


    });






